package com.dikeleakagedetection.myapplication.utils

import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Build
import android.provider.Settings

import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.startActivityForResult
import androidx.core.content.ContextCompat
import com.dikeleakagedetection.myapplication.R
import java.util.ArrayList

object CheckPermissionsUtils {

    public const val REQUEST_CODE_OPEN_GPS = 1
    public const val REQUEST_CODE_PERMISSION_LOCATION = 2

    //动态权限检查
    fun checkPermissions(activity: Activity) {
        //是否开启蓝牙，如果没开启，引导用户开启
        val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        if (!bluetoothAdapter.isEnabled) {
            R.string.please_open_blue.showToast()
            return
        }

        //动态权限申请
        val permissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
        val permissionDeniedList: MutableList<String> = ArrayList()
        for (permission in permissions) {
            val permissionCheck = ContextCompat.checkSelfPermission(activity, permission)
            if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
                onPermissionGranted(permission, activity)
            } else {
                permissionDeniedList.add(permission)
            }
        }
        if (permissionDeniedList.isNotEmpty()) {
            val deniedPermissions = permissionDeniedList.toTypedArray()
            ActivityCompat.requestPermissions(
                activity,
                deniedPermissions,
                REQUEST_CODE_PERMISSION_LOCATION
            )
        }

    }

    private fun onPermissionGranted(permission: String, activity: Activity) {
        when (permission) {
            Manifest.permission.ACCESS_FINE_LOCATION ->                 //版本号大于6.0且没有打开GPS
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !checkGPSIsOpen(activity)) {
                    //引导用户打开GPS
                    AlertDialog.Builder(activity)
                        .setTitle(R.string.notifyTitle)
                        .setMessage(R.string.gpsNotifyMsg)
                        .setNegativeButton(
                            R.string.cancel
                        ) { _, _ -> activity.finish() }
                        .setPositiveButton(
                            R.string.setting
                        ) { _, _ ->
                            val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                            startActivityForResult(activity, intent, REQUEST_CODE_OPEN_GPS, null)
                        }
                        .setCancelable(false)
                        .show()
                }
        }
    }

    fun checkGPSIsOpen(context: Context): Boolean {
        val locationManager = context.getSystemService(AppCompatActivity.LOCATION_SERVICE) as LocationManager
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
    }

}